package com.taomu.app.zongchouhuli.moudle.mine.activity;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.ColorRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.framework.BaseActivity;
import com.taomu.app.zongchouhuli.framework.BaseFragment;
import com.taomu.app.zongchouhuli.framework.BasePresenter;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.GetInfoContract;
import com.taomu.app.zongchouhuli.moudle.mine.contract.impl.GetInfoPresenter;
import com.taomu.app.zongchouhuli.moudle.mine.fragment.WalletListFragment;
import com.taomu.app.zongchouhuli.utils.DpUtils;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;

import java.util.ArrayList;
import java.util.List;

public class UserWalletActivity extends BaseActivity<GetInfoContract.Presenter> implements GetInfoContract.View {

    private ImageView mBackIm;

    private MagicIndicator indicator;

    private ViewPager mViewPager;

    private TextView mAccountTv;

    private TextView mPriceTv;

    private TextView mRechargeTv;

    private TextView mCashTv;

    private TextView mIncomeTv;

    private List<String> mTitles = new ArrayList<>();

    private List<BaseFragment> mFragments = new ArrayList<>();

    @Override
    protected void onResume() {
        mPresenter.getUserInfo();
        super.onResume();
    }

    @Override
    protected int getContentLayout() {
        return R.layout.activity_wallet;
    }

    @Override
    protected void initView() {
        mBackIm = findViewById(R.id.wallet_back_im);
        indicator = findViewById(R.id.wallet_indicator);
        mViewPager = findViewById(R.id.wallet_viewpager);
        mAccountTv = findViewById(R.id.wallet_account_tv);
        mPriceTv = findViewById(R.id.wallet_price_tv);
        mRechargeTv = findViewById(R.id.wallet_recharge_tv);
        mCashTv = findViewById(R.id.wallet_cash_tv);
        mIncomeTv = findViewById(R.id.wallet_income_tv);
    }

    @Override
    protected void initData() {
        //添加下划线
        mAccountTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);

        setIndicator();

    }

    @Override
    protected void initEvent() {
        mBackIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mAccountTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(UserAccountActivity.class);
            }
        });
        mCashTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(UserCashActivity.class);
            }
        });
        mRechargeTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(UserRechargeActivity.class);
            }
        });
    }

    @Override
    protected GetInfoContract.Presenter createPresenter() {
        return new GetInfoPresenter(this);
    }

    private WalletListFragment walletListFragment;

    private void setIndicator(){
        mTitles.add("总收入");
        mTitles.add("总支出");
        mTitles.add("充值");
        mTitles.add("提现");
        walletListFragment = WalletListFragment.getInstence("6");
        walletListFragment.setWalletListListener(new WalletListFragment.WalletListListener() {
            @Override
            public void onTotalMoney(String totalMoney) {
                if(TextUtils.isEmpty(totalMoney)){
                    mIncomeTv.setText("¥0");
                }else if("null".equals(totalMoney)){
                    mIncomeTv.setText("¥0");
                }else {
                    mIncomeTv.setText("¥" + totalMoney);
                }
            }
        });
        mFragments.add(walletListFragment);
        mFragments.add(WalletListFragment.getInstence("1"));
        mFragments.add(WalletListFragment.getInstence("3"));
        mFragments.add(WalletListFragment.getInstence("4"));
        if (mViewPager  == null) {
            return;
        }
        FragmentStatePagerAdapter adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {

            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }
        };
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(mFragments.size());
        initPagerIndicator(indicator,mViewPager);
        bind(indicator,mViewPager);
    }


//    private String getColor(@ColorRes int colorId){
//        int color = ResourcesCompat.getColor(getResources(), colorId, null);
//        String result = String.format("#%s", Integer.toHexString(color));
//        return result;
//    }

    public void bind(final MagicIndicator magicIndicator, ViewPager viewPager) {
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                magicIndicator.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            public void onPageSelected(int position) {
                magicIndicator.onPageSelected(position);
            }

            public void onPageScrollStateChanged(int state) {
                magicIndicator.onPageScrollStateChanged(state);
            }
        });
    }


    /**
     * 初始话导航栏
     * @param indicator
     * @param viewPager
     */
    private void initPagerIndicator(MagicIndicator indicator, final ViewPager viewPager){
        if (indicator == null) {
            return;
        }
        CommonNavigator navigator = new CommonNavigator(this);
        navigator.setBackgroundColor(getResources().getColor(R.color.base_white));
        navigator.setAdjustMode(true);
        navigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitles.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int i) {
                ColorTransitionPagerTitleView titleView = new ColorTransitionPagerTitleView(context);
                titleView.setText(mTitles.get(i));
                titleView.setNormalColor(getResources().getColor(R.color.text_normal_black));
                titleView.setSelectedColor(getResources().getColor(R.color.base_blue));
                titleView.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimensionPixelSize(R.dimen.text_28));
                titleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        resetState();
//                        bottomMenu.setVisibility(View.GONE);
//                        if(i == 1){
//                            clearTv.setVisibility(View.GONE);
//                        }else{
//                            clearTv.setVisibility(View.VISIBLE);
//                        }
                        viewPager.setCurrentItem(i);
                    }
                });
                return titleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(getResources().getColor(R.color.base_blue));
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineWidth(DpUtils.dp2px(20));
                return indicator;
            }
        });
        indicator.setNavigator(navigator);
    }

    @Override
    public void setUserInfo(ResUserInfoEntity entity) {
        mPriceTv.setText("¥"+entity.getAccount());
    }

    @Override
    public void setPresenter(GetInfoContract.Presenter presenter) {

    }
}
